import cv2
import numpy as np

# 读取图像
image = cv2.imread('input_data/src.jpg')

# 将图像转换为灰度图
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 应用形态学操作（闭运算：填充小孔，开运算：去除小噪声）
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  # 定义形态学核
filtered_image = cv2.morphologyEx(gray_image, cv2.MORPH_OPEN, kernel)  # 开运算
filtered_image = cv2.morphologyEx(filtered_image, cv2.MORPH_CLOSE, kernel)  # 闭运算

# 直方图均衡化
equalized_image = cv2.equalizeHist(filtered_image)

# 自适应阈值分割
threshold_image = cv2.adaptiveThreshold(
    equalized_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
    cv2.THRESH_BINARY, blockSize=11, C=2
)

# 将灰度图转换为彩色图（热图）
heatmap = cv2.applyColorMap(equalized_image, cv2.COLORMAP_BONE)

# 定义透明度
alpha = 0.5  # 半透明效果，0为完全透明，1为完全不透明

# 将热图叠加到原图上
result = cv2.addWeighted(image, 1, heatmap, alpha, 0)

# 显示结果
cv2.imshow('Original Image', image)
# cv2.imshow('Equalized Image', equalized_image)
cv2.imshow('Threshold Image', threshold_image)
cv2.imshow('Heatmap', heatmap)
cv2.imshow('Result Image', result)

# 等待按键按下后关闭所有窗口
cv2.waitKey(0)
cv2.destroyAllWindows()